package com.kele.goods.controller;

import com.kele.api.goods.BrandControllerApi;
import com.kele.common.model.response.PageResult;
import com.kele.common.model.response.Result;
import com.kele.goods.service.BrandService;
import com.kele.model.goods.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("brand")
//@CrossOrigin
public class BrandController implements BrandControllerApi {

    @Autowired
    private BrandService brandService;

    @GetMapping("findAll")
    public List<Brand> findAll(){
       /* int i =10;
        int j=i/0;*/
       return brandService.findAll();
    }

    @PostMapping("findPage/{pageNo}/{pageSize}")
    public PageResult<Brand> findPage(@PathVariable Integer pageNo,
                                      @PathVariable Integer pageSize,
                                      @RequestBody Map serachMap){
        return brandService.findPage(pageNo,pageSize,serachMap);
    }

    @PostMapping("save")
    public Result save(@RequestBody Brand brand){
        if(brand.getId()==null || brand.getId() == 0){
            return brandService.insert(brand);
        }else{
            return brandService.updateById(brand);
        }
    }

    @GetMapping("findById/{id}")
    public Brand findById(@PathVariable Long id){
        return brandService.selectById(id);
    }

    @GetMapping("deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        return brandService.deleteById(id);
    }

    @GetMapping("deleteByIds/{idList}")
    public Result deleteByIds(@PathVariable List<Long> idList){
        return brandService.deleteBatchByIds(idList);
    }

}
